package com.jeeplus.modules.monitor.task;

import com.jeeplus.modules.monitor.CheckCycleStart.entity.CheckMainentity;
import com.jeeplus.modules.monitor.CheckCycleStart.entity.CheckPlanTableTable;
import com.jeeplus.modules.monitor.CheckCycleStart.service.CheckCycleStartService;
import com.jeeplus.modules.monitor.entity.Task;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MyTest extends Task {
    @Autowired
    CheckCycleStartService checkCycleStartService;

    @Override
    public void run() {
        System.out.println("这是测试任务TestTask。");
        check();
        System.out.println("执行结束");
    }

    public void check() {
        List<CheckMainentity> list = checkCycleStartService.findList();
        Calendar cal = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();
        for (CheckMainentity a : list) {
            if (a.getBasicdataCheckJob() != null) {
                if (a.getBasicdataCheckJob().indexOf("1") != -1) {
                    if (!a.getBasicdataCheckCheck().equals("0")) {
                        Date date = new Date();
                        Date basicdataCheckTime = a.getBasicdataCheckTime();
                        cal.setTime(basicdataCheckTime);
                        a.getBasicdataCheckCheck();
                        a.getBasicdataCheckCheckUnit();
//                        cal.getTime().before(date1)
                        while (basicdataCheckTime.getMonth() > date.getMonth()+1) {
                            if (a.getBasicdataCheckCheckUnit().equals("D")) {
                                System.out.println("1D");
                                cal.add(Calendar.DATE, Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                            }
                            if (a.getBasicdataCheckCheckUnit().equals("M")) {
                                System.out.println("1M");
                                cal.add(Calendar.MONTH, Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                            }
                            if (a.getBasicdataCheckCheckUnit().equals("W")) {
                                System.out.println("1W");
                                cal.add(Calendar.DATE, 7 * Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                            }
                            basicdataCheckTime = cal.getTime();
                        }
                        cal1.setTime(date);
                        cal1.add(Calendar.MONTH, 1);
                        Date date1 = cal1.getTime();
                        while (cal.getTime().before(date1)) {
                            if (a.getBasicdataCheckCheckUnit().equals("D")) {
                                System.out.println("2D");
                                CheckPlanTableTable checkPlanTableTable = new CheckPlanTableTable();
                                checkPlanTableTable.setBasicdataCheckId(a.getId());
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                                String format = formatter.format(cal.getTime());
                                checkPlanTableTable.setStartTime(format);
                                cal.add(Calendar.DATE, Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                                checkPlanTableTable.setEndTime(cal.getTime());
                                checkPlanTableTable.setMark("未启动");
                                checkCycleStartService.insertCheckPlanTable(checkPlanTableTable);
                            }
                            if (a.getBasicdataCheckCheckUnit().equals("M")) {
                                System.out.println("2M");
                                CheckPlanTableTable checkPlanTableTable = new CheckPlanTableTable();
                                checkPlanTableTable.setBasicdataCheckId(a.getId());
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                                String format = formatter.format(cal.getTime());
                                checkPlanTableTable.setStartTime(format);
                                cal.add(Calendar.MONTH, Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                                checkPlanTableTable.setEndTime(cal.getTime());
                                checkPlanTableTable.setMark("未启动");
                                checkCycleStartService.insertCheckPlanTable(checkPlanTableTable);
                            }
                            if (a.getBasicdataCheckCheckUnit().equals("W")) {
                                System.out.println("2w");
                                CheckPlanTableTable checkPlanTableTable = new CheckPlanTableTable();
                                checkPlanTableTable.setBasicdataCheckId(a.getId());
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                                String format = formatter.format(cal.getTime());
                                checkPlanTableTable.setStartTime(format);
                                cal.add(Calendar.DATE, 7 * Integer.valueOf(a.getBasicdataCheckCheck()).intValue());
                                checkPlanTableTable.setEndTime(cal.getTime());
                                checkPlanTableTable.setMark("未启动");
                                checkCycleStartService.insertCheckPlanTable(checkPlanTableTable);
                            }
                            basicdataCheckTime = cal.getTime();
                        }
                    }
                    System.out.println(list);
                }
            }
        }
    }
}
